package com.ku.pksu.bornegaarden;

import java.util.concurrent.atomic.AtomicBoolean;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.ku.pksu.bornegaarden.info.Info;
import com.ku.pksu.bornegaarden.info.InfoActivityStack;
import com.ku.pksu.bornegaarden.lp.LPActivityStack;
import com.ku.pksu.bornegaarden.lp.Laereplan;
import com.ku.pksu.bornegaarden.social.SocialActivityStack;
import com.ku.pksu.bornegaarden.util.DownloadAsync;
import com.ku.pksu.bornegaarden.util.FetchListener;

public class Intro extends TabActivity implements OnClickListener, AnimationListener, FetchListener {

	private final String LAUNCHED = "launched";
	private RelativeLayout splashView;
	private Animation splash;
	private boolean isLaunched = false;
	private AtomicBoolean dataDownloaded = new AtomicBoolean();
	private Handler delay;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_alt);

		// Retrieve state
		if (savedInstanceState != null){
			isLaunched = savedInstanceState.getBoolean(LAUNCHED);
		}


		// Tabs
		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost();  // The activity TabHost
		TabHost.TabSpec spec;  // Resusable TabSpec for each tab
		Intent intent;  // Reusable Intent for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, LPActivityStack.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec(getString(R.string.laereplaner))
				.setIndicator(getString(R.string.laereplaner), res.getDrawable(android.R.drawable.ic_menu_add))
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, SocialActivityStack.class);
		spec = tabHost.newTabSpec(getString(R.string.nuogher))
				.setIndicator(getString(R.string.nuogher), res.getDrawable(android.R.drawable.ic_menu_add))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, InfoActivityStack.class);
		spec = tabHost.newTabSpec(getString(R.string.info))
				.setIndicator(getString(R.string.info), res.getDrawable(android.R.drawable.ic_menu_add))
				.setContent(intent);
		tabHost.addTab(spec);

		// Splash
		splash = AnimationUtils.loadAnimation(this, R.anim.splash);
		splashView = (RelativeLayout)findViewById(R.id.intro_splash_ref);
		splash.setAnimationListener(this);
		delay = new Handler();
		if (isLaunched){ //!isLaunched
//			delay.postDelayed(new Runnable() {
//
//				@Override
//				public void run() {
//					splashView.startAnimation(splash);
//				}
//			}, 2000);
//		} else {
			splashView.setVisibility(View.GONE);
		}

		// TODO Move to async task
		if (!isLaunched){
//			HTTPFetcher fetch = new HTTPFetcher(this);
//			String[] array = getResources().getStringArray(R.array.InfoItemLinks);
//			for(String s : array){
//				Log.e("Fetch", "Fetching "+s);
//				fetch.fetch(s.substring(0, s.length()-4));
//			}
			new DownloadAsync<Intro>().execute(this, this);
		}
		dataDownloaded.set(false);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.intro_knap_praktisk:
			Intent iPraktisk = new Intent(this, Info.class);
			startActivity(iPraktisk);
			break;
		case R.id.intro_knap_laereplan:
			Intent iLaereplan = new Intent(this, Laereplan.class);
			startActivity(iLaereplan);
			break;
		}

	}

	/*
	 * Abstract methods
	 */

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean(LAUNCHED, true);
	}

	@Override
	public void onAnimationEnd(Animation arg0) {
		splashView.setVisibility(View.GONE);
	}

	@Override
	public void onAnimationRepeat(Animation arg0) {
	}

	@Override
	public void onAnimationStart(Animation arg0) {
	}

	@Override
	public void downloadComplete() {
		// Advances the splash screen when files have been fetched		
		dataDownloaded.set(true);
		delay.postDelayed(new Runnable() {

			@Override
			public void run() {
				splashView.startAnimation(splash);
			}
		}, 1000);
	}
	
	public AtomicBoolean dataIsDownloaded(){
		return dataDownloaded;
	}
}